/***************************** BEGIN LICENSE BLOCK ***************************

 The contents of this file are subject to the Mozilla Public License Version
 1.1 (the "License"); you may not use this file except in compliance with
 the License. You may obtain a copy of the License at
 http://www.mozilla.org/MPL/MPL-1.1.html
 
 Software distributed under the License is distributed on an "AS IS" basis,
 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 for the specific language governing rights and limitations under the License.
 
 The Original Code is the "EO-SPS Framework".
 
 The Initial Developer of the Original Code is Spotimage S.A.
 Portions created by the Initial Developer are Copyright (C) 2007
 the Initial Developer. All Rights Reserved.
 
 Contributor(s): 
    Alexandre Robin <alexandre.robin@spotimage.fr>
 
******************************* END LICENSE BLOCK ***************************/

package com.spotimage.eosps;

import org.vast.ogc.gml.GMLException;
import org.vast.ogc.gml.GMLTimeWriter;
import org.vast.ows.OWSException;
import org.vast.ows.sps.SPSException;
import org.vast.ows.swe.SWEResponseWriter;
import org.w3c.dom.*;
import org.vast.util.TimeInfo;
import org.vast.xml.DOMHelper;


/**
 * <p><b>Title:</b><br/>
 * GetSensorAvailability Response Writer v2.0
 * </p>
 *
 * <p><b>Description:</b><br/>
 * Writer to generate an adequate XML response based on values
 * contained in a GetSensorAvailabilityResponse object for EOSPS v2.0
 * </p>
 *
 * <p>Copyright (c) 2008</p>
 * @author Alexandre Robin
 * @date Mar 04, 2008
 * @version 1.0
 */
public class GetSensorAvailabilityResponseWriterV20 extends SWEResponseWriter<GetSensorAvailabilityResponse>
{
	protected GMLTimeWriter gmlWriter = new GMLTimeWriter("3.2");
	
	
	public Element buildXMLResponse(DOMHelper dom, GetSensorAvailabilityResponse response, String version) throws OWSException
	{
		dom.addUserPrefix("eosps", EOSPSUtils.EOT_NAMESPACE);
		
		// root element
		Element rootElt = dom.createElement("eosps:GetSensorAvailabilityResponse");
		
		// write extensions
		writeExtensions(dom, rootElt, response);
		
		// responsePeriod
		try
		{
			Element timeElt = gmlWriter.writeTime(dom, response.getResponsePeriod());
			Element parentElt = dom.addElement(rootElt, "eosps:responsePeriod");
			parentElt.appendChild(timeElt);
		}
		catch (GMLException e)
		{
			throw new SPSException(e.getMessage(), e);
		}
		
		// list of availabilityPeriods
		try
		{
			for (TimeInfo timePeriod: response.getAvailabilityPeriods())
			{
				Element timeElt = gmlWriter.writeTime(dom, timePeriod);
				Element parentElt = dom.addElement(rootElt, "+eosps:availabilityPeriod");
				parentElt.appendChild(timeElt);
			}
		}
		catch (GMLException e)
		{
			throw new SPSException(e.getMessage(), e);
		}
		
		return rootElt;
	}
	
}